package ui2.player
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.MouseEvent;
   // greensock
   import com.greensock.TweenMax;
   import com.greensock.easing.Linear;
   // boy
   import ui2.MyPlayerMgr;

   /**
    * @author     cjboy | cjboy1984@gmail.com
    * @usage
    * 1. 首先，你必需先有個MyPlayerMgr的getter，以範例來說是取得第0個MyPlayerMgr。
    * private function get mgr():MyPlayerMgr { return MyPlayerMgr.getMgrAt(0); }
    *
    * 2. 再來利用mgr來監聽事件和用mgr來執行行為。
    * mgr.play();
    * mgr.pause();
    * ...
    */
   public class ProgressBar_1 extends MovieClip
   {
      // fla
      public var mcPlayhead:MovieClip;
      public var mcBuffer:MovieClip;

      // whether to resume play
      private var resumePlay:Boolean;

      // percentage of the seeker
      private var seekToPerc:Number;

      /* constructor */
      public function ProgressBar_1()
      {
         // disable tab-functionality.
         tabEnabled = false;
         tabChildren = false;
         focusRect = false;

         buttonMode = true;
         mouseChildren = false;

         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // ------------------ LINE -------------------

      // ################ Protected ################

      // ################# Private #################

      private function onAdd(e:Event):void
      {
         mcPlayhead.width = 0;
         mcBuffer.width = 0;

         // model
         mgr.addEventListener(MyPlayerMgr.BUFFERING, onUpdateBuffer);
         mgr.addEventListener(MyPlayerMgr.PLAYING, onUpdatePlayhead);
         mgr.addEventListener(MyPlayerMgr.VIDEO_END, onVideoEnd);

         // seeker functionality
         addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
      }

      private function onRemove(e:Event):void
      {
         // tween
         TweenMax.killTweensOf(mcPlayhead);
         TweenMax.killTweensOf(mcBuffer);

         // model
         mgr.removeEventListener(MyPlayerMgr.BUFFERING, onUpdateBuffer);
         mgr.removeEventListener(MyPlayerMgr.PLAYING, onUpdatePlayhead);
         mgr.removeEventListener(MyPlayerMgr.VIDEO_END, onVideoEnd);

         // seeker functionality
         removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      // ------------------ LINE -------------------

      // buffer note
      private function onUpdateBuffer(e:Event):void
      {
         var w:Number = width * mgr.loadPerc / 100;
         TweenMax.to(mcBuffer, 0.2, { width:w } );
      }

      // ------------------ LINE -------------------

      // playhead note
      private function onUpdatePlayhead(e:Event):void
      {
         var w:Number = width * mgr.playheadPercentage / 100;
         TweenMax.to(mcPlayhead, 0.2, { width:w, ease:Linear.easeNone } );
      }

      // video ends note
      private function onVideoEnd(e:Event):void
      {
         TweenMax.to(mcPlayhead, 0.2, { width:width, ease:Linear.easeNone } );
      }

      // ------------------ LINE -------------------

      private function onMDown(e:MouseEvent):void
      {
         // pause video
         resumePlay = mgr.playing;
         mgr.pause();

         // update progress bar
         seekToPerc = mouseX / width;
         var w:Number = width * seekToPerc;
         TweenMax.to(mcPlayhead, 0.2, { width:w } );

         // add drag & up handler
         stage.addEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      private function onMMove(e:MouseEvent):void
      {
         // update progress bar
         seekToPerc = mouseX / width;
         var w:Number = width * seekToPerc;
         TweenMax.to(mcPlayhead, 0.2, { width:w } );
      }

      private function onMUp(e:MouseEvent):void
      {
         // resume play
         mgr.seekPercentage(Math.floor(100 * seekToPerc));
         if (resumePlay) mgr.play();

         // remove drag & up handler
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      // ------------------ LINE -------------------

      private function get mgr():MyPlayerMgr { return MyPlayerMgr.getMgrAt(0); }

      // ------------------ LINE -------------------

   }

}